package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Reduce {

    List<Integer> arrayList;

    public boolean isEqual(List<Integer> arrayList1) {
        List<List<Integer>> list1 = Arrays.asList(arrayList1);
        List<List<Integer>> list2 = Arrays.asList(arrayList);
        return list1.containsAll(list2);
    }

    public Reduce(List<Integer> arrayList) {
        this.arrayList = arrayList;
    }

    public int getMaximum() {
        ArrayList<Integer> list = new ArrayList<>();
        Integer length = arrayList.toArray().length;
        for (Integer a:arrayList) {
            list.add(a);
            list.sort(Comparator.naturalOrder());
        }
        return list.get(length-1);
    }

    public double getMinimum() {
        ArrayList<Integer> list = new ArrayList<>();
        Integer length = arrayList.toArray().length;
        for (Integer a:arrayList) {
            list.add(a);
            list.sort(Comparator.naturalOrder());
        }
        return list.get(0);
    }

    public double getAverage() {
        double sum = 0;
        double average = 0;
        int length = arrayList.toArray().length;
        for (Integer a:arrayList) {
            sum = a + sum;
        }
        average = sum/length;
        return average ;
    }

    public double getOrderedMedian() {
        Integer length = arrayList.toArray().length;
        double k = 0;
        double m = 0;
        double a =0;
        double t =2.0;
        if (length%2==0){
            k = arrayList.get(length / 2 );
            m = arrayList.get(length / 2 - 1);
            a = (k + m)/t;

        }else {
            a = arrayList.get((length -1) /2);
        }
        return a;
    }

    public Double getMedianInLinkList(SingleLink singleLink) {
        Integer length = arrayList.toArray().length;
        double k = 0;
        double m = 0;
        double a =0;
        double t =2.0;
        if (length%2==0){
            k = arrayList.get(length / 2 );
            m = arrayList.get(length / 2 - 1);
            a = (k + m)/t;

        }else {
            a = arrayList.get((length -1) /2);
        }
        return a;
    }

    public int getFirstEven() {
        ArrayList<Integer> list = new ArrayList<>();
        for (Integer a:arrayList) {
            if(a%2==0){
                list.add(a);
            }
        }

        return list.get(0);
    }

    public int getIndexOfFirstEven() {
        ArrayList<Integer> list = new ArrayList<>();
        for (Integer a:arrayList) {
            if(a%2==0){
                int i = arrayList.indexOf(a);
                list.add(i);
            }
        }

        return list.get(0);
    }

    public int getLastOdd() {
        ArrayList<Integer> list = new ArrayList<>();
        for (Integer a:arrayList) {
            if(a%2!=0){

                list.add(a);
            }
        }

        return list.get(list.size()-1);
    }

    public int getIndexOfLastOdd() {
        ArrayList<Integer> list = new ArrayList<>();
        for (Integer a:arrayList) {
            if(a%2!=0){
                int i = arrayList.indexOf(a);
                list.add(i);
            }
        }

        return list.get(list.size()-1);
    }
}
